package com.zhq.blog.manage.domain.controller.product;

import java.util.ArrayList;
import java.util.List;

import javax.validation.Valid;

import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.zhq.blog.core.domain.product.Category;
import com.zhq.blog.manage.domain.controller.BaseController;
import com.zhq.blog.manage.domain.controller.BossModel;

@Controller
@Scope("prototype")
@RequestMapping("/admin/product/category/*")
public class CategoryController extends BaseController {
	
	@RequestMapping()
    public BossModel index(){
		return createModel("admin/product/category/list").add("category",list()).add("categorys", context.getCategoryRepository().findAll());
	}
	
	@RequestMapping(value="delete/{id}")
	public BossModel delete(@PathVariable long id){
		Category category = context.getCategoryRepository().findOne(id);
		if(category.getFlag() == 0){
			List<Category> categorys =  context.getCategoryRepository().getParent(id);
			if(categorys.size() > 0){
				for (int i = 0; i < categorys.size(); i++) {
					context.getCategoryRepository().delete(categorys.get(i).getId());
				}
			}
			
		}else{
			context.getCategoryRepository().delete(id);
		}
		return createModel("admin/product/category/list").add("category",list());
	}
	
	@RequestMapping(value = "saveCategory", method = RequestMethod.POST)
	public BossModel saveCategory(Category category){
		List<Category> skuList = category.getCategoryList(); 
		for (int i = 0; i < skuList.size(); i++) {
			Category cate = context.getCategoryRepository().findOne(skuList.get(i).getId());
			cate.setName(skuList.get(i).getName());
			cate.setSort(skuList.get(i).getSort());
			context.getCategoryRepository().save(cate);
		}
		return createModel("admin/product/category/list").add("category",list());
	}
	
	@RequestMapping(value="add")
	public BossModel add(){
		return createModel("admin/product/category/info").add("category",new Category());
	}
	
	@RequestMapping(value="save",method = RequestMethod.POST)
	public BossModel save(@Valid final Category category, final BindingResult bindingResult, final ModelMap model, RedirectAttributes redirectCategory,@PageableDefault(page = 0,size = DEFAULT_PAGE_SIZE) Pageable pageable){
		if (bindingResult.hasErrors()) {
			return createModel("admin/product/category/info");
		}
		if(category.getCategoryId() != 0){
			category.setFlag(1);
			category.init();
			context.getCategoryRepository().save(category);
		}else{
			category.setFlag(0);
			category.init();
			context.getCategoryRepository().save(category);
		}
		
		String message = category.getSucessFlashMessage();
		redirectCategory.addFlashAttribute("message", message);
		return createModel("redirect:/admin/product/category/index");
		
	}
	
	@RequestMapping(value="addSon/{id}")
	public BossModel addSon(@PathVariable long id){
		Category category = context.getCategoryRepository().findOne(id);
		return createModel("admin/product/category/info").add("category",new Category()).add("name", category.getName()).add("id", id);
	}
	
	private Category list(){
		Category Categoryss = new Category();
		List<Category> parent = context.getCategoryRepository().getParent();
		List<Category> cats = new ArrayList<Category>();
		
		for (int i = 0; i < parent.size(); i++) {
			cats.add(parent.get(i));
			List<Category> categorys =  context.getCategoryRepository().getParent(parent.get(i).getId());
			if(categorys.size()>0){
				for (int j = 0; j < categorys.size(); j++) {
					categorys.get(j).setCateName(parent.get(i).getName());
					cats.add(categorys.get(j));
				}
			}
			
		}
		Categoryss.setCategoryList(cats);
		return Categoryss;
	}
	

}
